#include "system.h"
#include "delay.h"
#include "lm303.h"
#include "i2c.h"

__CONFIG  (WDTDIS&MCLRDIS&INTIO&BORDIS&BORV21&LVPDIS);

unsigned int TMP;
unsigned int iTmp;

unsigned char TTT[20];
unsigned char AAA[20];

unsigned char VAL;

    union {
    signed int  ALL;
    struct{
      unsigned char  a;
      unsigned char  b;
    } PART;
    } STRUCT;



unsigned char H2A(unsigned char Val)
{
/*
switch (Val)
{

	case 0: return '0';
	case 1: return '1';
	case 2: return '2';
	case 3: return '3';
	case 4: return '4';
	case 5: return '5';
	case 6: return '6';
	case 7: return '7';
	case 8: return '8';
	case 9: return '9';	
	case 10:return 'A';
	case 11:return 'B';
	case 12:return 'C';
	case 13:return 'D';	
	case 14:return 'E';
	case 15:return 'F';

}
*/
return (Val+'A');
}




void main(void)
{
Init();

Led1 = Led2 = 1;
Led3 = 1;

RA7 = 1;

//RC0 = 0;


//i2c_Restart();
//i2c_Restart();
//i2c_SendByte(0x55);
//i2c_Stop();

SSPADD = 128;
SSPCON = 0;
SSPM0 = 0;
SSPM1 = 0;
SSPM2 = 0;
SSPM3 = 1;

SSPEN = 1;

//SaveReg(0X21, 0b10000000);
//DelayMs(200);
SaveReg(0X21, 0b00100000);
SaveReg(0X20, 0b00100111);
SaveReg(0X23, 0b01000000);

TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x0);
TTT[6] = i2c_m_PutByte(0b00010000);
i2c_m_Stop();

TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x1);
TTT[6] = i2c_m_PutByte(0b00100000);;
i2c_m_Stop();

TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x2);
TTT[6] = i2c_m_PutByte(0b00000001);;
i2c_m_Stop();



for(;;)
{
    
    RA7 ^= 1;
/*
while ((!TXIF)||(!TRMT)){}
TXREG = TTT[1];
while ((!TXIF)||(!TRMT)){}
TXREG = TTT[0];
while ((!TXIF)||(!TRMT)){}
TXREG = TTT[3];
while ((!TXIF)||(!TRMT)){}
TXREG = TTT[2];
while ((!TXIF)||(!TRMT)){}
TXREG = TTT[5];
while ((!TXIF)||(!TRMT)){}
TXREG = TTT[4];
*/

Led3 ^= 1;


i2c_read_array(0x28, 1, &AAA[0]);
i2c_read_array(0x29, 1, &AAA[1]);
i2c_read_array(0x2A, 1, &AAA[2]);
i2c_read_array(0x2B, 1, &AAA[3]);
i2c_read_array(0x2C, 1, &AAA[4]);
i2c_read_array(0x2D, 1, &AAA[5]);


    /*
TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x0);
TTT[6] = i2c_m_ReadFrom(0x3d);
TTT[6] = i2c_m_GetByte(FALSE);
i2c_m_Stop();

TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x1);
TTT[6] = i2c_m_ReadFrom(0x3d);
TTT[6] = i2c_m_GetByte(FALSE);
i2c_m_Stop();

TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x2);
TTT[6] = i2c_m_ReadFrom(0x3d);
TTT[6] = i2c_m_GetByte(FALSE);
i2c_m_Stop();
*/

/*
TTT[0] = i2c_m_WriteTo(0x3c);
TTT[0] = i2c_m_PutByte(0x03);
TTT[0] = i2c_m_ReadFrom(0x3d);
TTT[0] = i2c_m_GetByte(TRUE);
TTT[1] = i2c_m_GetByte(TRUE);
TTT[2] = i2c_m_GetByte(TRUE);
TTT[3] = i2c_m_GetByte(FALSE);
i2c_m_Stop();
*/


TTT[0] = i2c_m_WriteTo(0x3c);
TTT[0] = i2c_m_PutByte(0x3);
TTT[0] = i2c_m_ReadFrom(0x3d);
TTT[0] = i2c_m_GetByte(FALSE);
i2c_m_Stop();

TTT[1] = i2c_m_WriteTo(0x3c);
TTT[1] = i2c_m_PutByte(0x4);
TTT[1] = i2c_m_ReadFrom(0x3d);
TTT[1] = i2c_m_GetByte(FALSE);
i2c_m_Stop();

/*
TTT[2] = i2c_m_WriteTo(0x3c);
TTT[2] = i2c_m_PutByte(0x5);
TTT[2] = i2c_m_ReadFrom(0x3d);
TTT[2] = i2c_m_GetByte(FALSE);
i2c_m_Stop();

TTT[3] = i2c_m_WriteTo(0x3c);
TTT[3] = i2c_m_PutByte(0x6);
TTT[3] = i2c_m_ReadFrom(0x3d);
TTT[3] = i2c_m_GetByte(FALSE);
i2c_m_Stop();
*/

TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x2);
TTT[6] = i2c_m_PutByte(0b00000001);;
i2c_m_Stop();


/*
TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x4);
TTT[6] = i2c_m_ReadFrom(0x3d);
TTT[6] = i2c_m_GetByte(FALSE);
i2c_m_Stop();

TTT[6] = i2c_m_WriteTo(0x3c);
TTT[6] = i2c_m_PutByte(0x9);
TTT[6] = i2c_m_ReadFrom(0x3d);
TTT[6] = i2c_m_GetByte(FALSE);
i2c_m_Stop();
*/

TXREG = 0b10000000;
DelayMs(1);       
TXREG = 0;
DelayMs(1);       
STRUCT.PART.b=AAA[0];
STRUCT.PART.a=AAA[1];
STRUCT.ALL /=16;
STRUCT.ALL += 2048;
STRUCT.ALL /=32;
VAL = STRUCT.ALL;
TXREG = VAL;
DelayMs(1);       


TXREG = 0b10000000;
DelayMs(1);       
TXREG = 1;
DelayMs(1);       
STRUCT.PART.b=AAA[2];
STRUCT.PART.a=AAA[3];
STRUCT.ALL /= 16;
STRUCT.ALL += 2048;
STRUCT.ALL /= 32;
VAL = STRUCT.ALL;
TXREG = VAL;
DelayMs(1);       


TXREG = 0b10000000;
DelayMs(1);       
TXREG = 2;
DelayMs(1);       
STRUCT.PART.b=TTT[0];
STRUCT.PART.a=TTT[1];
STRUCT.ALL += 2048;
STRUCT.ALL /= 32;
VAL = STRUCT.ALL;
TXREG = VAL;
DelayMs(1);       


/*
TXREG = TTT[0];
DelayMs(1);       
TXREG = TTT[1];
DelayMs(1);       
*/






/*
TXREG = '>';
DelayMs(1);              

TXREG = H2A(TTT[0]>>4);
DelayMs(1);
TXREG = H2A(TTT[0]&0b00001111);
DelayMs(1);
TXREG = H2A(TTT[1]>>4);
DelayMs(1);
TXREG = H2A(TTT[1]&0b00001111);
DelayMs(1);

TXREG = H2A(TTT[2]>>4);
DelayMs(1);
TXREG = H2A(TTT[2]&0b00001111);
DelayMs(1);
TXREG = H2A(TTT[3]>>4);
DelayMs(1);
TXREG = H2A(TTT[3]&0b00001111);
DelayMs(1);


TXREG = H2A(TTT[4]>>4);
DelayMs(1);
TXREG = H2A(TTT[4]&0b00001111);
DelayMs(1);
TXREG = H2A(TTT[5]>>4);
DelayMs(1);
TXREG = H2A(TTT[5]&0b00001111);
DelayMs(1);




TXREG = H2A(AAA[0]>>4);
DelayMs(1);
TXREG = H2A(AAA[0]&0b00001111);
DelayMs(1);
TXREG = H2A(AAA[1]>>4);
DelayMs(1);
TXREG = H2A(AAA[1]&0b00001111);
DelayMs(1);

TXREG = H2A(AAA[2]>>4);
DelayMs(1);
TXREG = H2A(AAA[2]&0b00001111);
DelayMs(1);
TXREG = H2A(AAA[3]>>4);
DelayMs(1);
TXREG = H2A(AAA[3]&0b00001111);
DelayMs(1);


TXREG = H2A(AAA[4]>>4);
DelayMs(1);    
TXREG = H2A(AAA[4]&0b00001111);
DelayMs(1);
TXREG = H2A(AAA[5]>>4);
DelayMs(1);
TXREG = H2A(AAA[5]&0b00001111);
DelayMs(1);


TXREG = '\r';
DelayMs(1);
TXREG = '\n';
DelayMs(1);
*/


/*
TXREG = TTT[1];
DelayMs(20);
TXREG = TTT[0];
DelayMs(20);
TXREG = TTT[3];
DelayMs(20);
TXREG = TTT[2];
DelayMs(20);
TXREG = TTT[5];
DelayMs(20);
TXREG = TTT[4];
DelayMs(500);
*/
//DelayMs(50);
RB6 = 0;
//RB7^=1;

}
}
